package com.anlu.yue.config;

import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.ResponseErrorHandler;

import java.io.IOException;

public class CustomResponseErrorHandler implements ResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        return response.getStatusCode().is4xxClientError() ||
                response.getStatusCode().is5xxServerError();
    }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        if (response.getStatusCode().is5xxServerError()) {
            throw new RuntimeException("服务器错误: " + response.getStatusCode());
        } else if (response.getStatusCode().is4xxClientError()) {
            throw new RuntimeException("客户端错误: " + response.getStatusCode());
        }
    }
}

